import {
  ViewEngineBase,
  ViewController,
  IEditFormController,
  SysUIActionTag,
  EventBase,
  IOptViewState,
  IOptViewEvent,
} from '@ibiz-template/runtime';
import { IAppDEEditView } from '@ibiz/model-core';

export class OptViewEngine extends ViewEngineBase {
  /**
   * 视图控制器
   *
   * @protected
   * @type {ViewController<IAppDEEditView, IOptViewState, IOptViewEvent>}
   * @memberof OptViewEngine
   */
  protected declare view: ViewController<
    IAppDEEditView,
    IOptViewState,
    IOptViewEvent
  >;

  /**
   * 表单部件
   *
   * @readonly
   * @memberof OptViewEngine
   */
  get form(): IEditFormController {
    return this.view.getController('form') as IEditFormController;
  }

  /**
   * 视图created生命周期执行逻辑
   *
   * @return {*}  {Promise<void>}
   * @memberof OptViewEngine
   */
  async onCreated(): Promise<void> {
    await super.onCreated();
    const { childNames, modal } = this.view;
    childNames.push('form');

    modal.hooks.shouldDismiss.tapPromise(async context => {
      if (this.form.state.modified) {
        const isAllow = await ibiz.modal.confirm({
          title: '关闭提醒',
          desc: '表单数据已经修改，确定要关闭？',
        });
        if (!isAllow) {
          context.allowClose = false;
          // return null;
        }
      }
    });
  }

  /**
   * 视图mounted生命周期执行逻辑
   *
   * @return {*}  {Promise<void>}
   * @memberof OptViewEngine
   */
  async onMounted(): Promise<void> {
    await super.onMounted();
    const { model, evt } = this.view;

    // 监控form事件
    const formDeId = this.form.model.appDataEntityId;
    const formDataStateChange = (event: EventBase): void => {
      const data = event.data[0];
      this.toolbar?.calcButtonState(data, formDeId);
      if (data.srfkey) {
        evt.emit('onViewInfoChange', { dataInfo: data.srfmajortext });
      }
    };

    this.form.evt.on('onLoadSuccess', event => {
      formDataStateChange(event);
    });
    this.form.evt.on('onLoadDraftSuccess', event => {
      formDataStateChange(event);
    });
    this.form.evt.on('onSaveSuccess', event => {
      formDataStateChange(event);
    });

    if (!this.view.state.noLoadDefault && model.loadDefault) {
      this.load();
    }
  }

  /**
   * 获取数据
   *
   * @return {*}  {IData[]}
   * @memberof OptViewEngine
   */
  getData(): IData[] {
    return this.form.getData();
  }

  /**
   * 加载
   *
   * @memberof OptViewEngine
   */
  load(): Promise<IData> {
    return this.form.load();
  }

  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
  async call(key: string, args: any): Promise<IData | null | undefined> {
    if (key === SysUIActionTag.CANCEL) {
      this.cancel();
      return null;
    }
    if (key === SysUIActionTag.OK) {
      await this.confirm();
      return null;
    }
    return super.call(key, args);
  }

  /**
   * 确认
   *
   * @memberof OptViewEngine
   */
  async confirm(): Promise<void> {
    await this.form.save();
    this.view.closeView({ ok: true, data: this.getData() });
  }

  /**
   * 取消
   *
   * @memberof OptViewEngine
   */
  cancel(): void {
    this.view.closeView({ ok: false, data: [] });
  }
}
